// 这个模块是路由规则的模块,为了左侧菜单显示导航
import { constantRoutes, asyncRoutes } from '@/router'
export default {
  state: {
    routes: constantRoutes
  },
  mutations: {
    setRoutes(state, newRoutes) {
      state.routes = [...constantRoutes, ...newRoutes, { path: '*', redirect: '/404', hidden: true }]
    },
    // 重置路由规则
    resetRoutes(state) {
      state.routes = [...constantRoutes, { path: '*', redirect: '/404', hidden: true }]
    }
  },
  actions: {
    // 优化 - 将过滤的操作也优化的vuex中
    getFilterRoutes(context, res) {
      const filterRoutes = asyncRoutes.filter(item => res.roles.menus.includes(item.children[0].name))
      context.commit('setRoutes', filterRoutes)
      return filterRoutes // 过滤出来的路由规则也需要返回
    }
  },
  namespaced: true
}
